﻿//option.maodian 是否在切换时，更新锚点，如果你的tab不用iframe,且有1个以上tab页要用分页控件，则这个值等于true
//如果主页有分页也有tab页，则这个属性会影响主页的分页。
var tabHelper = function (option) {
    var titles = option.titleFun();
    var titleAndContent = [];
    var titlestr = "";
    var temstr = "";
    for (var i = 0; i < titles.length; i++) {
        if (i == 0) {
            temstr += "<li class='" + option.currentClassName + "'>" + titles[i] + "</li>";
        } else {
            temstr += "<li>" + titles[i] + "</li>";
        }
    }
    $("#" + option.titleID).html(temstr);
    var temtitleel = $("#" + option.titleID).find("li");
    temtitleel.bind("click", function(event) {
        temtitleel.each(function(index, el) {
            el.className = "";
        });
        var tabLi = event.target;
        if (tabLi.nodeName.toUpperCase() !== "LI") {
            tabLi = $(tabLi).parent("li").get(0);
        }
        tabLi.className = option.currentClassName;
        var contentel = $("#" + option.contentID);
        var temwidth = contentel.width();
        var temheight = contentel.height();
        contentel.html("");
        if (!option.isIframe) {
            contentel.append("<img id='tabmenuload' style='position:absolute;z-index:999;display:block' src='/static/app/img/loading.gif'/>");
        }
        var tabmenuload = $('#tabmenuload');
        //tabmenuload.css('top', contentel.offset().top + temheight / 2);
        //tabmenuload.css('left', contentel.offset().left + temwidth / 2);
        //contentel.width(temwidth);
        contentel.height(temheight);
        var currentEvent = tabLi;
        var url = option.urlFun(tabLi.innerHTML);
        if (url.indexOf("?")!=-1) {
            url += "&random=" + Math.random();
        } else {
            url += "?random=" + Math.random();
        }
        
        var data = option.dataFun(tabLi.innerHTML);
        if (!data) {
            data = "temdata=" + Math.random();
        }
        if (option.isIframe) {
            var thisdiv = $("#" + option.contentID);
            var tempHeight = "";
            if (option.isIframeHeight) {
                tempHeight = 'height="' + option.isIframeHeight + ' px"';
            }
            var h = thisdiv.parent().find(".tab-hd").height();
            thisdiv.parent().css("position", "relative");
            thisdiv.parent().append('<img src="/static/app/img/loading.gif" class="center-loading" style="position:absolute;left:0;top:' + h + 'px"; />');
            thisdiv.html("<iframe frameborder='0' " + tempHeight + " id='tabmenuiframe' scrolling='no' allowtransparency='yes' width='100%' src='" + url + "' />");

            function heightauto() {
                try {
                    var mainheight = $("#tabmenuiframe").contents().height();

                    $("#tabmenuiframe").height(mainheight);
                } catch (e) {
                }
            }
            //暂时而为，因为系统中有些场景，如角色管理，不支持自动匹配，原因不明，所以加个参数
            $("#tabmenuiframe").bind("load", function () {
                $(this).parent().parent().find($(".center-loading")).remove();
                heightauto();
                if (option.autoMatch) {
                    setInterval(heightauto, 500);
                }
            });
        } else {
            if (tabHelper.firstclick == 0) {
            } else {
                if (option.maodian) {
                    document.location.hash = "maodian";
                    $(window).unbind("hashchange");
                }
            }
            tabHelper.firstclick = 1;
            
            $.ajax({
                url: url,
                data: data,
                type: 'GET',
                success: function(reqdata) {
                    if (currentEvent.className != option.currentClassName) {
                        return;
                    }
                    $("#" + option.contentID).html(reqdata);
                    var temurlarray = url.split('/');
                    var jsclass = temurlarray[temurlarray.length - 1].split('?')[0];
                    jsclass = jsclass.replace(/-/g, "");
                    try {
                        eval(jsclass + ".init()");
                    } catch (e) {
                        //   alert("error:"+e);
                    }
                },
                error: function(e) {
                    $("#" + option.contentID).html("加载失败:" + e.responseText + "status:" + e.status + "readyState:" + e.readyState);
                }
            });
        }
    });
    if (temtitleel.length > 0) {
        temtitleel.first().click();
    }
};
tabHelper.firstclick = 0;